package org.zwy.gradetrack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.zwy.gradetrack.entity.GraduateRequirementAchievement;
import org.zwy.gradetrack.mapper.GraduateRequirementAchievementMapper;
import org.zwy.gradetrack.service.GraduateRequirementAchievementService;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class GraduateRequirementAchievementServiceImpl extends ServiceImpl<GraduateRequirementAchievementMapper, GraduateRequirementAchievement> implements GraduateRequirementAchievementService {

    @Override
    public void calculateComprehensiveScore(GraduateRequirementAchievement achievement) {
        BigDecimal directScore = achievement.getDirectEvaluation().multiply(new BigDecimal("0.7"));
        BigDecimal teacherScore = achievement.getTeacherEvaluation() != null ?
                achievement.getTeacherEvaluation().multiply(new BigDecimal("0.1")) : BigDecimal.ZERO;
        BigDecimal internshipScore = achievement.getInternshipEvaluation() != null ?
                achievement.getInternshipEvaluation().multiply(new BigDecimal("0.1")) : BigDecimal.ZERO;
        BigDecimal selfScore = achievement.getSelfEvaluation().multiply(new BigDecimal("0.1"));

        BigDecimal comprehensiveScore = directScore.add(teacherScore)
                .add(internshipScore)
                .add(selfScore)
                .setScale(4, RoundingMode.HALF_UP);

        achievement.setComprehensiveScore(comprehensiveScore);
        achievement.setIsAchieved(determineAchievement(comprehensiveScore));
    }

    @Override
    public boolean determineAchievement(BigDecimal comprehensiveScore) {
        return comprehensiveScore.compareTo(new BigDecimal("0.6")) >= 0;
    }
}